package com.jichangxiu.server.platform.action;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import com.jichangxiu.common.annotation.*;
import com.jichangxiu.common.entity.bo.PageData;
import com.jichangxiu.framework.action.BaseAction;
import com.jichangxiu.server.platform.entity.dto.PlatformTenantNameDto;
import com.jichangxiu.server.platform.entity.dto.PlatformTenantQueryDto;
import com.jichangxiu.server.platform.entity.po.PlatformTenant;
import com.jichangxiu.server.platform.entity.vo.PlatformTenantAddVo;
import com.jichangxiu.server.platform.entity.vo.PlatformTenantEditVo;
import com.jichangxiu.server.platform.entity.vo.PlatformTenantQueryVo;
import com.jichangxiu.server.platform.service.PlatformTenantService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;

/**
 * PlatformTenantAction:
 * 平台-租户管理控制器
 *
 * @author Jcx
 * @create 2023-03-15 18:23
 */
@Slf4j
@Rest("/platform/tenant")
@Tag(name = "平台-租户管理控制器")
public class PlatformTenantAction extends BaseAction {

    @Resource
    private PlatformTenantService platformTenantService;

    /**
     * 新增租户
     *
     * @param platformTenantAddVo 租户视图对象
     * @return 是否成功
     */
    @Post("/add")
    @Operation(summary = "平台-新增租户")
    @PreAuthorize("@ss.hasPermission('platform:tenant:add')")
    public String add(@RequestBody PlatformTenantAddVo platformTenantAddVo) {
        return toStr(platformTenantService.add(platformTenantAddVo));
    }

    /**
     * 多选级联删除租户
     *
     * @param tenantIds 租户ID列表
     * @return 是否成功
     */
    @Delete("/del")
    @Operation(summary = "平台-多选级联删除租户")
    @PreAuthorize("@ss.hasPermission('platform:tenant:del')")
    public String del(@RequestBody String[] tenantIds) {
        return toStr(platformTenantService.del(ListUtil.toList(tenantIds)));
    }

    /**
     * 修改租户信息
     *
     * @param platformTenantEditVo 租户视图对象
     * @return 是否成功
     */
    @Put("/edit")
    @Operation(summary = "平台-修改租户信息")
    @PreAuthorize("@ss.hasPermission('platform:tenant:edit')")
    public String edit(@RequestBody PlatformTenantEditVo platformTenantEditVo) {
        return toStr(platformTenantService.edit(platformTenantEditVo));
    }

    /**
     * 根据ID获取租户详情
     *
     * @param tenantId 租户ID
     * @return 租户详情
     */
    @Get("/info/{tenantId}")
    @Operation(summary = "平台-根据ID获取租户详情")
    @PreAuthorize("@ss.hasPermission('platform:tenant:info')")
    public PlatformTenantQueryDto info(@PathVariable("tenantId") @NotBlank(message = "租户ID不能为空") String tenantId) {
        return platformTenantService.info(tenantId);
    }

    /**
     * 无分页查询租户名列表
     *
     * @return 租户名列表
     */
    @Get("/listAllTenantName")
    @Operation(summary = "平台-无分页查询租户名列表")
    public List<PlatformTenantNameDto> listAllTenantName() {
        List<PlatformTenant> list = platformTenantService.list();
        List<PlatformTenantNameDto> result = new ArrayList<>();
        if (ObjectUtil.isNotEmpty(list)) {
            list.forEach(platformTenant -> result.add(PlatformTenantNameDto.builder().tenantName(platformTenant.getTenantName()).build()));
            return result;
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * 分页查询租户列表
     *
     * @param platformTenantQueryVo 租户视图对象
     * @return 分页租户列表
     */
    @Get("/pageList")
    @Operation(summary = "平台-分页查询租户列表")
    @PreAuthorize("@ss.hasPermission('platform:tenant:pageList')")
    public PageData<PlatformTenantQueryDto> pageList(@ParameterObject PlatformTenantQueryVo platformTenantQueryVo) {
        return platformTenantService.pageList(platformTenantQueryVo);
    }

}
